package com.bml.architect.framework.protocol.dubbo;

import com.alibaba.fastjson.JSONObject;
import com.bml.architect.framework.Invocation;
import com.bml.architect.framework.register.LocalRegister;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("开始调用...");
        Invocation invocation = JSONObject.parseObject(msg.toString(), Invocation.class);
        Class<?> register = LocalRegister.getRegister(invocation.getInterfaceName());
        Method method = register.getMethod(invocation.getMethodName(), invocation.getParamTypes());
        Object invoke = method.invoke(register.newInstance(), invocation.getParams());
        System.out.println("调用完成...");
        ctx.writeAndFlush(invoke.toString());

    }
}
